package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserVehicleSkill;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserVehicleSkillDaoImpl extends TempDataObjectDao<UserVehicleSkill> {

	public Map<Integer, UserVehicleSkill> getUserVehicleSkillMap(long userId, String vehicleUid) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_vehicle_skill where userId=? and vehicleUid = ?";
		Map<Integer, UserVehicleSkill> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			call.setString(2, vehicleUid);
			rs = call.executeQuery();
			while (rs.next()) {
				UserVehicleSkill data = parse(rs);
				dataMap.put(data.getSkillPos(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<String, Map<Integer, UserVehicleSkill>> getUserVehicleSkillMap(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_vehicle_skill where userId=?";
		Map<String, Map<Integer, UserVehicleSkill>> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserVehicleSkill data = parse(rs);
				if (!dataMap.containsKey(data.getVehicleUid())) {
					dataMap.put(data.getVehicleUid(), new ConcurrentHashMap<>());
				}
				dataMap.get(data.getVehicleUid()).put(data.getSkillPos(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(UserVehicleSkill data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_vehicle_skill(userId,vehicleUid,skillPos,skillType,value,skillModifyTimes,randomSkillType,randomValue,invalid)" +
				" VALUES(?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getVehicleUid());
			call.setInt(i++, data.getSkillPos());
			call.setInt(i++, data.getSkillType());
			call.setInt(i++, data.getValue());
			call.setInt(i++, data.getSkillModifyTimes());
			call.setInt(i++, data.getRandomSkillType());
			call.setInt(i++, data.getRandomValue());
			call.setBoolean(i++, data.isInvalid());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserVehicleSkill data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_vehicle_skill SET skillType=?,value=?,skillModifyTimes=?,randomSkillType=?,randomValue=?, invalid = ? WHERE userId=? "
				+ "and vehicleUid=? and skillPos=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getSkillType());
			call.setInt(i++, data.getValue());
			call.setInt(i++, data.getSkillModifyTimes());
			call.setInt(i++, data.getRandomSkillType());
			call.setInt(i++, data.getRandomValue());
			call.setBoolean(i++, data.isInvalid());
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getVehicleUid());
			call.setInt(i++, data.getSkillPos());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserVehicleSkill parseData(ResultSet rs) throws SQLException {
		UserVehicleSkill data = new UserVehicleSkill();
		data.setUserId(rs.getLong("userId"));
		data.setVehicleUid(rs.getString("vehicleUid"));
		data.setSkillPos(rs.getInt("skillPos"));
		data.setSkillType(rs.getInt("skillType"));
		data.setValue(rs.getInt("value"));
		data.setSkillModifyTimes(rs.getInt("skillModifyTimes"));
		data.setRandomSkillType(rs.getInt("randomSkillType"));
		data.setRandomValue(rs.getInt("randomValue"));
		data.setInvalid(rs.getBoolean("invalid"));
		return data;
	}
}
